package com.esiran.payadmin.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class CalendarUtilEx {
    public static Date getNow(){
        Calendar calendar = Calendar.getInstance();
        return calendar.getTime();
    }
    public static Date add4NowByMonth(int amount){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,amount);
        return calendar.getTime();
    }
    public static Date add4NowByDay(int amount){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,amount);
        return calendar.getTime();
    }
    public static Date add4NowByHour(int amount){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR_OF_DAY,amount);
        return calendar.getTime();
    }
    public static Date add4NowByMin(int amount){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE,amount);
        return calendar.getTime();
    }
    public static Date add4NowBySec(int amount){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND,amount);
        return calendar.getTime();
    }
    public static String[] todayRange(){
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String today1 = simpleDateFormat.format(date);
        String today2 = simpleDateFormat.format(add4NowByDay(1));
        return new String[]{today1,today2};
    }
    public static String[] monthRange(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE,1);
        Date startDate = calendar.getTime();
        int maxDate = calendar.getActualMaximum(Calendar.DATE);
        calendar.set(Calendar.DATE,maxDate);
        Date endDate = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDateStr = simpleDateFormat.format(startDate);
        String endDateStr = simpleDateFormat.format(endDate);
        return new String[]{startDateStr,endDateStr};
    }
    public static String[] lastMonthRange(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE,1);
        calendar.add(Calendar.MONTH,-1);
        Date startDate = calendar.getTime();
        int maxDate = calendar.getActualMaximum(Calendar.DATE);
        calendar.set(Calendar.DATE,maxDate);
        calendar.add(Calendar.DATE,1);
        Date endDate = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDateStr = simpleDateFormat.format(startDate);
        String endDateStr = simpleDateFormat.format(endDate);
        return new String[]{startDateStr,endDateStr};
    }

    public static Date[] resolveDateRange(String[] dateRange) throws ParseException {
        if (dateRange == null || dateRange.length < 2){
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        return new Date[]{simpleDateFormat.parse(dateRange[0]),simpleDateFormat.parse(dateRange[1])};

    }
    public static String simpleFormat(String pattern){
        return simpleFormat(new Date(System.currentTimeMillis()),pattern);
    }
    public static String simpleFormat(Date date,String pattern){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(date);
    }

    public static String simpleFormat(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }
    public static String simpleFormatDate(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }
    public static String simpleFormatTime(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        return simpleDateFormat.format(date);
    }
}
